import Vue from 'vue'

import pUtils from '@/common/utils/public.js'
import chartsUtils from '@/common/utils/chartsUtils.js'
import formatTime from '@/common/filters.js'
const chartSW = function(listData, strNote, LineColor, max_min_Name) {
	var m = [];

	var chartName = []; //控件元素名称
	var chartTM = []; //时间序列
	var chartValue = []; //时间序列
	var liststr = ""; //拼装表格

	for (var i = 0; i < strNote.length; i++) {
		chartName.push(strNote[i].name);
	}

	for (var n = 0; n < listData.length; n++) {
		var charthan = []; //时间序列
		for (var k = 0; k < strNote.length; k++) {
			charthan.push(listData[n][strNote[k].codename]); //加入集合
			if (strNote[k].name == "时间") {
				chartTM.push(formatTime.convertToDate(listData[n][strNote[k].codename]).Format("MM-DD hh:mm"));
			} else {
				chartValue.push(pUtils.changeTwoDecimal(listData[n][strNote[k].codename], 2));
			}
		}
		m.push(charthan); //加入集合
	}

	var option = {
		//backgroundColor: '#100E19',
		tooltip: {
			trigger: 'axis'
		},
		color: LineColor,
		legend: {
			data: chartName,
			itemWidth: 8,
			itemHeight: 8,
			textStyle: {
				color: '#000',
				fontSize: 12
			}
			//控制过程线默认是否显示
			//       ,
			//		 selected: {
			//          '警戒水位' : false
			//      }
		},
		toolbox: {
			show: false,
			feature: {
				mark: {
					show: false
				},
				dataView: {
					show: false,
					readOnly: false
				},
				magicType: {
					show: false,
					type: ['line', 'bar']
				},
				restore: {
					show: false
				},
				saveAsImage: {
					show: false
				}
			}
		},
		calculable: true,
		xAxis: [{
			type: 'category',
			boundaryGap: false,
			data: chartTM,
			axisLine: {
				lineStyle: {
					color: '#918F8F',
					width: 1, //这里是为了突出显示加上的
				},
				textStyle: {
					color: '#000',
					fontSize: '16'
				}
			}
		}],
		yAxis: [{
			name: "水位(m)",
			type: 'value',
			boundaryGap: false,
			splitNumeber: 5,
			scale: true, //是否自动计算最大最小值。
			splitLine: {
				show: true,
				lineStyle: {
					color: '#eee',
					width: 1,
					type: 'dashed',
				}
			},
			//min:max_min.min, //动态设置最大值最小值。
			//max:max_min.max,
			min: function(value) {
				//			    return value.min - 0.5;
				if (value.max - value.min > 0.6)
					return value.min - (value.max - value.min) / 4;
				else if (value.max - value.min > 0.2)
					return value.min - (value.max - value.min);
				else
					return value.min - 0.1;
				//			    return value.min - (value.max - value.min);
			},
			max: function(value) {
				//			    return value.max + 0.5;
				if (value.max - value.min > 0.6)
					return value.max + (value.max - value.min) / 4;
				else if (value.max - value.min > 0.2)
					return value.max + (value.max - value.min);
				else
					return value.max + 0.1;
				//			    return value.max + (value.max - value.min) / 4;
				//			    return value.max + (value.max - value.min);
			},
			axisLabel: {
				formatter: function(v) {
					return v.toFixed(2);
				}
			},
			axisLine: {
				lineStyle: {
					color: '#918F8F',
					width: 1, //这里是为了突出显示加上的
					shadowBlur: 0,
					shadowOffsetX: 0,
				},
				textStyle: {
					color: '#000',
					fontSize: '16'
				},
			}
		}],
		series: function() {
			var serie = [];

			for (var j = 0; j < chartName.length; j++) {
				if (chartName[j] == "时间") //调过时间字段
				{
					continue;
				}
				var chartValue = []; //声明过线value集合
				for (var i = 0; i < m.length; i++) {
					chartValue.push(pUtils.changeTwoDecimal(m[i][j], 2));
				}
				if (max_min_Name != "" & max_min_Name == chartName[j]) {
					var item = {
						name: chartName[j],
						type: 'line',
						data: chartValue,
						markPoint: {
							data: [{
								type: 'max',
								name: '最大值'
							}, {
								type: 'min',
								name: '最小值'
							}]
						},
						connectNulls: true,
						smooth: true,
						itemStyle: {
							normal: {
								lineStyle: {
									width: 1,
									type: 'solid' //'dotted'虚线 'solid'实线
								}
							}
						}
					}
					serie.push(item);
				} else if (chartName[j] == "下游水位") {
					var item = {
						name: chartName[j],
						type: 'line',
						data: chartValue,
						markPoint: {
							data: [{
								type: 'max',
								name: '最大值'
							}, {
								type: 'min',
								name: '最小值'
							}]
						},
						connectNulls: true,
						smooth: true,
						itemStyle: {
							normal: {
								lineStyle: {
									width: 1,
									type: 'solid' //'dotted'虚线 'solid'实线
								}
							}
						}
					}
					serie.push(item);
				} else if (chartName[j] == "警戒水位" || chartName[j] == "保证水位") {
					var item = {
						name: chartName[j],
						type: 'line',
						data: chartValue,
						smooth: false,
						itemStyle: {
							normal: {
								lineStyle: {
									width: 1,
									type: 'dotted' //'dotted'虚线 'solid'实线
								}
							}
						}
					}
					serie.push(item);
				} else {
					var item = {
						name: chartName[j],
						type: 'line',
						data: chartValue,
						connectNulls: true,
						smooth: true
					}
					serie.push(item);
				}
			};
			return serie;
		}()
	};
	return option;
}

const chartYL = function(listData, strNote, LineColor, max_min_Name) {
	var chartName = []; //控件元素名称
	var chartTM = []; //时间序列
	var chartValue = []; //时间序列
	var liststr = ""; //拼装表格
	var m = [];
	
	for (var i = 0; i < strNote.length; i++) {
		chartName.push(strNote[i].name);
	}
	
	for (var n = listData.length - 1 ; n >= 0; n--) {
		var charthan = []; //时间序列
		for (var k = 0; k < strNote.length; k++) {
			charthan.push(listData[n][strNote[k].codename]); //加入集合
			if (strNote[k].name == "时间") {
				chartTM.push(formatTime.convertToDate(listData[n][strNote[k].codename]).Format("MM-DD hh:mm"));
			} else {
				chartValue.push(pUtils.changeTwoDecimal(listData[n][strNote[k].codename], 1));
			}
		}
		m.push(charthan); //加入集合
	}

	var option = {
		tooltip: {
			trigger: 'axis'
		},
		color: LineColor,
		legend: {
			data: chartName,
			itemWidth: 8,
			itemHeight: 8,
			textStyle: {
				color: '#000',
				fontSize: 12
			}
		},
		toolbox: {
			show: false,
			feature: {
				mark: {
					show: false
				},
				dataView: {
					show: false,
					readOnly: false
				},
				magicType: {
					show: false,
					type: ['line', 'bar']
				},
				restore: {
					show: false
				},
				saveAsImage: {
					show: false
				}
			}
		},
		calculable: true,
		xAxis: [{
			type: 'category',
			//          boundaryGap : false,
			data: chartTM,
			axisLine: {
				lineStyle: {
					color: '#918F8F',
					width: 1, //这里是为了突出显示加上的
				},
				textStyle: {
					color: '#000',
					fontSize: '16'
				}
			}
		}],
		yAxis: [{
			name: "雨量(mm)",
			type: 'value',
			boundaryGap: false,
			splitNumeber: 5,
			scale: true, //是否自动计算最大最小值。
			axisLabel: {
				formatter: function(v) {
					return v.toFixed(1);
				}
			},
			max: function(value) {
				//			    return value.max + 0.5;
				if (value.max - value.min > 1)
					return value.max + (value.max - value.min) / 2;
				else
					return 1;
				//			    return value.max + (value.max - value.min) / 4;
				//			    return value.max + (value.max - value.min);
			}
		}],
		series: function() {
			var serie = [];
			for (var j = 0; j < chartName.length; j++) {
				if (chartName[j] == "时间") //调过时间字段
				{
					continue;
				}
				var chartValue = []; //声明过线value集合
				for (var i = 0; i < m.length; i++) {
					chartValue.push(pUtils.changeTwoDecimal(m[i][j], 1)); //循环价值
				}
				if (max_min_Name != "" & max_min_Name == chartName[j]) {
					var item = {
						name: chartName[j],
						type: 'bar',
						data: chartValue,
						smooth: true
					}
					serie.push(item);
				} else {
					var item = {
						name: chartName[j],
						type: 'line',
						data: chartValue,
						smooth: true
					}
					serie.push(item);
				}
			};
			return serie;
		}()
	};
	return option;
}

export default {
	chartSW,
	chartYL
}
